#include "pwm8_cfg.h"

void set_pwm8_cfg(uint32_t pwmx)
{

	// 计算周期和预分频
	uint32_t timer_clk = SystemCoreClock; // 72MHz
	uint32_t prescaler = 0;
	uint32_t period = 0;
	// 设定最大周期，防止溢出
	uint32_t target_freq = 0;
	switch (pwmx)
	{
	case PWM1_FLAG:
		target_freq = machine.pwm1_freq;
		break;
	case PWM2_FLAG:
		target_freq = machine.pwm2_freq;
		break;
	case PWM3_FLAG:
		target_freq = machine.pwm3_freq;
		break;
	case PWM4_FLAG:	
	target_freq = machine.pwm4_freq;
		break;	
	case PWM5_FLAG:
		target_freq = machine.pwm5_freq;
		break;	
	case PWM6_FLAG:
		target_freq = machine.pwm6_freq;
		break;
	case PWM7_FLAG:
		target_freq = machine.pwm7_freq;
		break;	
	case PWM8_FLAG:	
		target_freq = machine.pwm8_freq;
		break;
	default:
		break;
	}
	// 预分频最大值65535，周期最大值65535
	prescaler = (timer_clk / target_freq / 65536);
	if (prescaler > 65535)
	{
		prescaler = 65535;
	}
	period = (timer_clk / (target_freq * (prescaler + 1))) - 1;
	if (period > 65535)
	{
		period = 65535;
	}
	switch (pwmx)
	{
	case PWM1_FLAG:
		__HAL_TIM_SET_PRESCALER(&htim2, prescaler);
		__HAL_TIM_SET_AUTORELOAD(&htim2, period);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, (uint32_t)((period + 1) * machine.pwm1_duty) - 1);
		//
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, (uint32_t)((period + 1) * machine.pwm2_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, (uint32_t)((period + 1) * machine.pwm3_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, (uint32_t)((period + 1) * machine.pwm4_duty) - 1);
		break;
	case PWM2_FLAG:
		__HAL_TIM_SET_PRESCALER(&htim2, prescaler);
		__HAL_TIM_SET_AUTORELOAD(&htim2, period);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, (uint32_t)((period + 1) * machine.pwm2_duty) - 1);
		//
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, (uint32_t)((period + 1) * machine.pwm1_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, (uint32_t)((period + 1) * machine.pwm3_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, (uint32_t)((period + 1) * machine.pwm4_duty) - 1);
		break;
	case PWM3_FLAG:
		__HAL_TIM_SET_PRESCALER(&htim2, prescaler);
		__HAL_TIM_SET_AUTORELOAD(&htim2, period);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, (uint32_t)((period + 1) * machine.pwm3_duty) - 1);
		//
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, (uint32_t)((period + 1) * machine.pwm1_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, (uint32_t)((period + 1) * machine.pwm2_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, (uint32_t)((period + 1) * machine.pwm4_duty) - 1);
		break;
	case PWM4_FLAG:
		__HAL_TIM_SET_PRESCALER(&htim2, prescaler);
		__HAL_TIM_SET_AUTORELOAD(&htim2, period);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, (uint32_t)((period + 1) * machine.pwm4_duty) - 1);
		//
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, (uint32_t)((period + 1) * machine.pwm1_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, (uint32_t)((period + 1) * machine.pwm2_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, (uint32_t)((period + 1) * machine.pwm3_duty) - 1);
		break;
	case PWM5_FLAG:
		__HAL_TIM_SET_PRESCALER(&htim1, prescaler);
		__HAL_TIM_SET_AUTORELOAD(&htim1, period);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, (uint32_t)((period + 1) * machine.pwm5_duty) - 1);
		//
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, (uint32_t)((period + 1) * machine.pwm6_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, (uint32_t)((period + 1) * machine.pwm7_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_4, (uint32_t)((period + 1) * machine.pwm8_duty) - 1);
		break;
	case PWM6_FLAG:
		__HAL_TIM_SET_PRESCALER(&htim1, prescaler);
		__HAL_TIM_SET_AUTORELOAD(&htim1, period);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, (uint32_t)((period + 1) * machine.pwm6_duty) - 1);
		//
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, (uint32_t)((period + 1) * machine.pwm5_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, (uint32_t)((period + 1) * machine.pwm7_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_4, (uint32_t)((period + 1) * machine.pwm8_duty) - 1);
		break;
	case PWM7_FLAG:
		__HAL_TIM_SET_PRESCALER(&htim1, prescaler);
		__HAL_TIM_SET_AUTORELOAD(&htim1, period);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, (uint32_t)((period + 1) * machine.pwm7_duty) - 1);
		//
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, (uint32_t)((period + 1) * machine.pwm5_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, (uint32_t)((period + 1) * machine.pwm6_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_4, (uint32_t)((period + 1) * machine.pwm8_duty) - 1);
		break;
	case PWM8_FLAG:
		__HAL_TIM_SET_PRESCALER(&htim1, prescaler);
		__HAL_TIM_SET_AUTORELOAD(&htim1, period);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_4, (uint32_t)((period + 1) * machine.pwm8_duty) - 1);
		//
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, (uint32_t)((period + 1) * machine.pwm5_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, (uint32_t)((period + 1) * machine.pwm6_duty) - 1);
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, (uint32_t)((period + 1) * machine.pwm7_duty) - 1);
		break;
	default:
		break;
	}

}
